//
//  ReportDescPop.m
//  iiecg
//
//  Created by Shanlin on 2017/3/3.
//  Copyright © 2017年 Boundary Technology. All rights reserved.
//

#import "ReportDescPop.h"
#import "HWPopTool.h"

@interface ReportDescPop ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *closeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *popBGHConstraint;
@property (weak, nonatomic) IBOutlet UIView *popBGView;
@property (weak, nonatomic) IBOutlet UILabel *reportTitleLabel;
@property (weak, nonatomic) IBOutlet UITextView *reportDescTextView;
@end

@implementation ReportDescPop

-(void)awakeFromNib{
    [super awakeFromNib];
    __weak typeof(self)weakSelf = self; 
    weakSelf.popBGView.layer.masksToBounds = YES;
    weakSelf.popBGView.layer.cornerRadius = 8;
    weakSelf.closeButton.tintColor = ECG_COLOR_GREEN;
    _reportDescTextView.font = [UIFont systemFontOfSize:17];
    
}

+(instancetype)createWith:(NSString*)title desc:(NSString*)desc
{
    ReportDescPop *view = [[NSBundle mainBundle] loadNibNamed:@"ReportDescPop" owner:self options:nil].firstObject;
    view.frame= CGRectMake(0, 0, KScreenW, KScreenH);
    view.reportTitleLabel.text = title;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSForegroundColorAttributeName : COLOR_BLACK_707070,
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    view.reportDescTextView.attributedText = [[NSAttributedString alloc] initWithString:desc attributes:attributes];
//    view.reportDescTextView.text = desc;
    CGFloat maxHeight = KScreenH - 110;
    CGFloat textHeight = [IKPublicUtil heightForString:desc fontSize:17 andWidth:KScreenW - 80] + 10;
    CGFloat allHeight = textHeight + 70 + 160;
    if (allHeight > maxHeight) {
        view.popBGHConstraint.constant = maxHeight;
        
    }else{
        view.popBGHConstraint.constant = allHeight;
    }
    //设置显示第一行
    view.reportDescTextView.layoutManager.allowsNonContiguousLayout = NO;
    
    return view;
}

-(void)show:(BOOL)animate
{
    __weak typeof(self)weakSelf = self;
    [HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeNone;
    [[HWPopTool sharedInstance] showWithPresentView:weakSelf animated:animate];
}
-(void)dismiss:(void(^)())complete
{
    [[HWPopTool sharedInstance] closeWithBlcok:complete];
}
- (IBAction)closeAction:(UIButton *)sender {
    [self dismiss:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    BOOL iscontain = CGRectContainsPoint(self.popBGView.frame, point);
    if (!iscontain)
    {
        [self dismiss:nil];
    }
}



@end
